package com.leo.ordermessage.comsumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class GlobalOrderConsumer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_grp_11_03");
        consumer.setNamesrvAddr("localhost:9876");
        consumer.subscribe("tp_demo_11_01", "*");

        consumer.setConsumeThreadMin(1);
        consumer.setConsumeThreadMax(1);
        consumer.setPullBatchSize(1);
        consumer.setConsumeMessageBatchMaxSize(1);
        //consumer.setMaxReconsumeTimes();
        //consumer.setPullInterval();

        //消费者本地缓存消息大小超过100M (流控)
        //consumer.setPullThresholdSizeForQueue(100);
        //消费者本地缓存消息数超过1000条 (流控)
        //consumer.setPullThresholdForQueue(1000);
        //消费者本地缓存消息跨度, 默认2000 (流控)
        //consumer.setConsumeConcurrentlyMaxSpan(2000);

        consumer.setMessageListener(new MessageListenerOrderly() {
            @Override
            public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
                for (MessageExt msg : msgs) {
                    System.out.println(new String(msg.getBody()));
                }
                return ConsumeOrderlyStatus.SUCCESS;
                //return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;
            }
        });
        consumer.start();
    }
}